package com.hqyj.ctxp.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.ctxp.mapper.UserMapper;
import com.hqyj.ctxp.pojo.Role;
import com.hqyj.ctxp.pojo.User;
import com.hqyj.ctxp.service.UserService;
import com.hqyj.ctxp.utils.JwtUtils;
import com.hqyj.ctxp.utils.ResultInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    //用户注册
    @Override
    public ResultInfo register(User user) {
        return insertUser(user);
    }

    //用户登录
    @Override
    public ResultInfo login(User user) {
        User userLogin = userMapper.login(user.getUserNo(), user.getUserPwd());
        if(userLogin != null){
            String userToken = JwtUtils.createUserToken(user);
            return new ResultInfo(200,"登录成功",userToken);
        }
        return new ResultInfo(500,"用户名或密码错误");
    }

    //用户列表
    @Override
    public ResultInfo selectUserList(User user) {
        Page<User> page =  new Page<>(user.getCurrentPage(),user.getPageSize());
        Page<User> userPage = userMapper.selectUserList(user.getUserName(),page);
        return new ResultInfo(200,"用户列表查询成功",userPage);
    }

    //查单个用户
    @Override
    public ResultInfo selectOne(String userName) {
        User user = userMapper.selectOne(new QueryWrapper<User>().
                eq("user_name", userName));
        return new ResultInfo(200,"查询数据成功",user);
    }
    @Override
        public ResultInfo selectOne(Integer userNo) {
        User user = userMapper.selectOne(new QueryWrapper<User>().
                eq("user_no", userNo));
        return new ResultInfo(200,"查询数据成功",user);
    }

    //角色列表
    @Override
    public ResultInfo selectRoles() {
        List<Role> roleList = userMapper.selectRoles();
        return new ResultInfo(200,"角色列表查询成功",roleList);
    }

    //根据用户编号修改用户角色
    @Override
    public ResultInfo changeUserRoleByUserNo(Integer userFkRole, Integer userNo) {
        Integer num = userMapper.changeUserRoleByUserNo(userFkRole, userNo);
        if(num>0){
            return new ResultInfo(200,"角色更新成功",num);
        }
        return new ResultInfo(500,"角色更新失败");
    }

    //根据用户编号删除用户信息
    @Override
    @Transactional
    public ResultInfo deleteUserByUserNo(Integer userNo) {
        //先删除用户信息表的对应用户信息，再删除用户表对应的用户
        int num = userMapper.deleteUserInfoByUserNo(userNo);
        int num1 = userMapper.delete(new QueryWrapper<User>()
                .eq("user_no", userNo));
        if(num > 0 && num1 >0){
            return new ResultInfo(200,"删除成功",num);
        }
        return new ResultInfo(500,"删除失败");
    }

    //新增用户同时在用户信息表增加该用户
    @Override
    @Transactional
    public ResultInfo insertUser(User user) {
        //查询是否存在该用户名
        QueryWrapper<User> nameQueryWrapper = new QueryWrapper<>();
        nameQueryWrapper.eq("user_name",user.getUserName());
        User user1 = userMapper.selectOne(nameQueryWrapper);
        //查询是否存在该用户编号
        QueryWrapper<User> noQueryWrapper = new QueryWrapper<>();
        noQueryWrapper.eq("user_no",user.getUserNo());
        User user2 = userMapper.selectOne(noQueryWrapper);
        // 如果不存在该用户，就添加
        if(user1==null && user2==null){
            int num = userMapper.insert(user);
            int num1 = userMapper.insertUserInfo(user.getUserName());
            if(num>0 && num1>0){
                return new ResultInfo(200,"success");
            }
        }
        if(user1!=null){
            return new ResultInfo(500,"该用户名已存在");
        }
        if(user2!=null){
            return new ResultInfo(500,"该用户帐号已存在");
    }
        return new ResultInfo(500,"failed");
    }

    //验证用户密码
    @Override
    public ResultInfo checkPwd(User user) {
        String msg = userMapper.checkPwd(user);
        if(msg!=null){
            return new ResultInfo(200,"密码正确");
        }
        return new ResultInfo(500,"密码错误");
    }

    //前台用户修改个人密码
    @Override
    public ResultInfo updateUserPwd(User user) {
        user.setUserPwd(user.getUserPwd());
        int num = userMapper.update(user, new UpdateWrapper<User>().eq("user_name", user.getUserName())
                .set("user_pwd", user.getUserPwd()));
        if(num>0) {
            return new ResultInfo(200, "修改密码成功，请重新登录");
        }
        return new ResultInfo(500,"修改密码失败");
    }

}
